﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Collections;

namespace sqxt
{
    public partial class ResidentAdd : Form
    {
        string PictureName = "";
        string ftpUrl = "localhost";
        DataTable dt = new DataTable();
        SqlDataAdapter sda;

        public ResidentAdd()
        {
            InitializeComponent();
        }
        bool xiugai = false;
        string hno = "";
        public ResidentAdd(bool b, string s)
        {
            xiugai = b;
            hno = s;
            InitializeComponent();
        }
        SqlConnection conn = new SqlConnection(Dbconnect.s1);
        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.Rows.Clear();
            }
            dataGridView1.DataSource = dataGridView1;
            SqlCommand sc = conn.CreateCommand();

            if (comboBoxEx1.Text == "")
            {
                MessageBox.Show("请选择检索条件！");
                return;
            }
            if (textBoxX1.Text == "")
            {
                MessageBox.Show("请输入查询信息！");
                return;
            }
            switch (comboBoxEx1.Text.Trim())
            {
                case "按姓名":
                    string strSql = "select person_name 姓名,person_sex 性别,person_id_no 证件号码, person_healthfile_no 档案编号  from person where  person_name='" + textBoxX1.Text.Trim() + "'";
                    sda = new SqlDataAdapter(strSql, conn);
                    sda.Fill(dt);
                    conn.Close();
                    if (dt.Rows.Count != 0)
                    {

                        bindingSource1.DataSource = dt;
                        dataGridView1.DataSource = bindingSource1;
                        dataGridView1.Visible = true;
                    }
                    else
                    {
                        tB_Name.Text = textBoxX1.Text.Trim();
                    }
                    break;
                case "按证件号码":
                    string strSq2 = "select person_name 姓名,person_sex 性别,person_id_no 证件号码, person_healthfile_no 档案编号 from person where  person_id_no='" + textBoxX1.Text.Trim() + "'";
                    sda = new SqlDataAdapter(strSq2, conn);
                    sda.Fill(dt);
                    conn.Close();
                    if (dt.Rows.Count != 0)
                    {
                        bindingSource1.DataSource = dt;
                        dataGridView1.DataSource = bindingSource1;
                        dataGridView1.Visible = true;
                    }
                    else
                    {
                        tB_CardNo.Text = textBoxX1.Text.Trim();
                    }
                    break;

            }
        }



        private void PicClear_Click(object sender, EventArgs e)
        {
            // pictureBox1.Image = "";
        }

        private void tB_Name_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                cB_Sex.Focus();
                e.Handled = true;
            }
        }

        private void bt_Save_Click(object sender, EventArgs e)    //保存
        {

            #region    修改居民时触发

            if (xiugai)
            {

                try
                {
                    if (isend != "归档")
                    {
                        SqlDataAdapter sda = new SqlDataAdapter("select person_name,person_sex,person_birthdate,person_idtype,person_id_no,person_nation,person_family_tel,person_livetype,Person_addr,person_healthfile_no,Person_Medside,create_doctor,duty_doctor,youbian,person_iscreate,person_createdate,person_isend from person", Dbconnect.s1);
                        DataTable dtxg = new DataTable();
                        sda.Fill(dtxg);
                        dtxg.PrimaryKey = new DataColumn[] { dtxg.Columns[9] };
                        DataRow dr = dtxg.Rows.Find(textBoxX5.Text.Trim());
                        dr["person_name"] = tB_Name.Text;
                        dr["person_sex"] = cB_Sex.Text;
                        dr["person_birthdate"] = dT_Birth.Text;
                        dr["person_idtype"] = cB_CardType.Text;
                        dr["person_id_no"] = tB_CardNo.Text;
                        dr["person_nation"] = cB_Nation.Text;
                        dr["person_family_tel"] = textBoxX6.Text;
                        dr["person_livetype"] = comboBoxEx2.Text;
                        dr["Person_addr"] = textBoxX2.Text;
                        dr["Person_Medside"] = comboBoxEx9.Text;
                        dr["create_doctor"] = comboBoxEx11.Text;
                        dr["duty_doctor"] = comboBoxEx10.Text;
                        dr["youbian"] = textBoxX4.Text;
                        SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                        sda.Update(new DataRow[] { dr });
                        MessageBox.Show("已成功修改");
                    }
                    else
                    {
                        MessageBox.Show("该居民档案已终结，不可再进行操作");
                    }

                }
                catch (Exception e1)
                {
                    
                }
            }

            #endregion
            else
            {
                bool idpd = false;
                if (cB_CardType.Text.Trim() == "身份证")
                {
                    conn.Open();
                    string yzid = "select * from person  where person_idtype='身份证'  and  person_id_no='" + tB_CardNo.Text + "'";
                    SqlCommand yz = new SqlCommand(yzid, conn);
                    SqlDataReader idadr = yz.ExecuteReader();
                    idpd = idadr.HasRows;
                    conn.Close();
                }
                if (idpd)
                {
                    MessageBox.Show("该身份证号码已经被登记过，请检查是否有误");

                }
                else
                {

                    if (tB_Name.Text.Trim() == "" || cB_Sex.Text.Trim() == "" || cB_Nation.Text.Trim() == " " || dT_Birth.Text.Trim() == "" || cB_CardType.Text.Trim() == "" || tB_CardNo.Text.Trim() == "" || comboBoxEx10.Text.Trim() == "" || comboBoxEx9.Text.Trim() == "" || textBoxX2.Text=="")
                    {
                        MessageBox.Show("请把必填信息补充完整！");
                    }
                    else
                    {
                        try
                        {

                            SqlDataAdapter sda = new SqlDataAdapter("select person_name,person_sex,person_birthdate,person_idtype,person_id_no,person_nation,person_family_tel,person_livetype,Person_addr,person_healthfile_no,Person_Medside,create_doctor,duty_doctor,youbian,person_iscreate,person_createdate,person_isend from person", Dbconnect.s1);
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                            dt.PrimaryKey = new DataColumn[] { dt.Columns["person_healthfile_no"] };
                            DataRow dr;
                            dr = dt.NewRow();
                            dr["person_name"] = tB_Name.Text;
                            dr["person_sex"] = cB_Sex.Text;
                            dr["person_birthdate"] = dT_Birth.Text;
                            dr["person_idtype"] = cB_CardType.Text;
                            dr["person_id_no"] = tB_CardNo.Text;
                            dr["person_nation"] = cB_Nation.Text;
                            dr["person_family_tel"] = textBoxX6.Text;
                            dr["person_livetype"] = comboBoxEx2.Text;
                            dr["Person_addr"] = textBoxX2.Text;
                            textBoxX5.Text = AutoNumber.getAutoNumber(comboBoxEx9.Text.Trim());
                            dr["person_healthfile_no"] = textBoxX5.Text;
                            dr["Person_Medside"] = comboBoxEx9.Text;
                            dr["create_doctor"] = comboBoxEx11.Text;
                            dr["duty_doctor"] = comboBoxEx10.Text;
                            dr["youbian"] = textBoxX4.Text;
                            dr["person_iscreate"] = "未建档";
                            dr["person_createdate"] = DateTime.Now.ToShortDateString();
                            dr["person_isend"] = "未归档";
                            dt.Rows.Add(dr);
                            SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                            sda.Update(dt);


                            beadroll bd = (beadroll)this.Owner;
                            conn.Open();
                            string sql = "select  person_name 姓名, person_sex 性别,person_idtype  证件类型,person_id_no 证件号码,person_healthfile_no  档案编号,person_birthdate 出生日期,person_family_tel  家庭电话,person_iscreate 建档状态,Person_addr 家庭地址 from  person  where  person_healthfile_no='" + textBoxX5.Text + "'";
                            SqlDataAdapter sdafh = new SqlDataAdapter(sql, conn);
                            DataTable dtfh = new DataTable();
                            sdafh.Fill(dtfh);
                            conn.Close();
                            bindingSource1.DataSource = dtfh;
                            bd.dataGridViewX1.DataSource = bindingSource1;

                            #region 满足条件在老年人或儿童表插入一条数据


                            int age = DateTime.Now.Year - Convert.ToDateTime(dT_Birth.Value.ToShortDateString()).Year;
                            if (age > 59)
                            {
                                string insertStr = "insert into elderly (person_healthfile_no,is_end) " + " values ('" + textBoxX5.Text + "','未完成')";


                                using (conn = new SqlConnection(Dbconnect.s1))
                                {
                                    try
                                    {
                                        SqlCommand cmd = new SqlCommand(insertStr, conn);
                                        if (conn.State != ConnectionState.Open) conn.Open();
                                        cmd.ExecuteNonQuery();
                                        conn.Close();
                                        string ts = "居民 " + tB_Name.Text + "属于老年人范围，已添加到老年人民册";
                                        remine_num rm = new remine_num(ts, 4);
                                        rm.Show();
                                    }
                                    catch (SqlException e1)
                                    {
                                        
                                    }
                                }

                                //if (age < 4)
                                //{
                                //    string insertc = "insert into children (person_healthfile_no,is_end) " + " values ('" + textBoxX5.Text + "','未完成')";
                                //    using (conn = new SqlConnection(Dbconnect.s1))
                                //    {
                                //        try
                                //        {
                                //            SqlCommand cmd = new SqlCommand(insertc, conn);
                                //            if (conn.State != ConnectionState.Open) conn.Open();
                                //            cmd.ExecuteNonQuery();
                                //            conn.Close();
                                //            string ts = "居民 " + tB_Name.Text + "属于0-3岁儿童范围，已添加到儿童民册";
                                //            remine_num rm = new remine_num(ts, 4);
                                //            rm.Show();
                                //        }
                                //        catch (SqlException sqlex)
                                //        {
                                //            MessageBox.Show(sqlex.Message.ToString());
                                //        }
                                //    }
                                //}
                            }
                            #endregion
                        }
                        catch (Exception)
                        {
                        }
                            

                    }
                }

            }


            this.Close();
        }

        public void ResidentAdd_Load(object sender, EventArgs e)
        {

            dataGridView1.Visible = false;
            #region 读取居委
            ArrayList arr = new ArrayList();
            SqlCommand com = new SqlCommand("select MedSite_name from medsite ", conn);
            conn.Open();
            SqlDataReader sdr = com.ExecuteReader();
            while (sdr.Read())
            {

                arr.Add(sdr.GetString(0));
            }
            foreach (string s in arr)
            {
                comboBoxEx9.Items.Add(s);
            }
            sdr.Close();
            conn.Close();
            #endregion
            #region   读取医生信息
            ArrayList arr1 = new ArrayList();
            SqlCommand com1 = new SqlCommand("select empcode from employee ", conn);
            conn.Open();
            SqlDataReader sdr1 = com1.ExecuteReader();
            while (sdr1.Read())
            {

                arr1.Add(sdr1.GetString(0));
            }
            foreach (string s in arr1)
            {
                comboBoxEx10.Items.Add(s);
                comboBoxEx11.Items.Add(s);
            }
            sdr1.Close();
            conn.Close();

            this.comboBoxEx10.SelectedIndex = 0;
            this.comboBoxEx11.SelectedIndex = 0;
            #endregion
            #region 读取名族
            ArrayList arrmz = new ArrayList();
            SqlCommand commz = new SqlCommand("select nation from nation ", conn);
            conn.Open();
            SqlDataReader sdrmz = commz.ExecuteReader();
            while (sdrmz.Read())
            {

                arrmz.Add(sdrmz.GetString(0));
            }
            foreach (string s in arrmz)
            {
                cB_Nation.Items.Add(s);
            }
            sdrmz.Close();
            conn.Close();
            #endregion
            cB_CardType.Items.Add("身份证");
            cB_CardType.Items.Add("军官证");
            cB_CardType.Items.Add("护照");
            cB_CardType.Items.Add("回乡证");
            cB_CardType.Items.Add("其他");
            comboBoxEx2.Items.Add("户籍");
            comboBoxEx2.Items.Add("非户籍");
            if (xiugai) //修改居民时读出信息
            {
                labelX7.Visible = false;
                comboBoxEx1.Visible = false;
                textBoxX1.Visible = false;
                buttonX1.Visible = false;
                read(hno);
            }

        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            string healthno = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
            read(healthno);
        }

        string isend = "";//档案是否终结
        private void read(string hno)  //读取选中居民的信息显示出来 传入档案编号
        {
            try
            {
                conn.Open();
                string cx = "select person_name,person_sex,person_birthdate,person_idtype,person_id_no,person_nation,person_family_tel,person_livetype,Person_addr,person_healthfile_no,Person_Medside,create_doctor,duty_doctor,youbian,person_isend from person where  person_healthfile_no='" + hno.Trim() + "'";
                SqlCommand sc = new SqlCommand(cx, conn);
                SqlDataReader sdr = sc.ExecuteReader();
                sdr.Read();
                {
                    tB_Name.Text = sdr[0] is DBNull ? string.Empty : sdr.GetString(0);
                    cB_Sex.Text = sdr[1] is DBNull ? string.Empty : sdr.GetString(1);
                    cB_Nation.Text = sdr[5] is DBNull ? string.Empty : sdr.GetString(5);
                    dT_Birth.Text = sdr[2].ToString();
                    cB_CardType.Text = sdr[3] is DBNull ? string.Empty : sdr.GetString(3);
                    tB_CardNo.Text = sdr[4] is DBNull ? string.Empty : sdr.GetString(4);
                    comboBoxEx9.Text = sdr[10] is DBNull ? string.Empty : sdr.GetString(10);
                    textBoxX2.Text = sdr[8] is DBNull ? string.Empty : sdr.GetString(8);
                    comboBoxEx2.Text = sdr[7] is DBNull ? string.Empty : sdr.GetString(7);
                    textBoxX4.Text = sdr[13] is DBNull ? string.Empty : sdr.GetString(13);
                    textBoxX6.Text = sdr[6] is DBNull ? string.Empty : sdr.GetString(6);
                    comboBoxEx10.Text = sdr[12] is DBNull ? string.Empty : sdr.GetString(12);
                    textBoxX5.Text = sdr[9] is DBNull ? string.Empty : sdr.GetString(9);
                    comboBoxEx11.Text = sdr[11] is DBNull ? string.Empty : sdr.GetString(11);
                    isend = sdr["person_isend"].ToString().Trim();
                    dataGridView1.Visible = false;

                }
                conn.Close();
                sdr.Close();
            }
            catch (Exception e1)
            {
                
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)//显示地址
        {
            bool d = true;
            addr ad = new addr(d);
            ad.Owner = this;
            ad.ShowDialog();
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tB_CardNo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (char)13)
            {
                if (cB_CardType.Text == "身份证")
                {
                    if (tB_CardNo.Text.Trim().Length != 15 || tB_CardNo.Text.Trim().Length != 15)
                        MessageBox.Show("身份证位数不对");
                }
            }


        }



    }
}
